package Petgoods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/*
总控制台，一级菜单
按1是买家界面，只能查看商品，购买商品，查看余额，充值余额（表为buyer）
按2是卖家界面，只能上架商品（表为seller）
按3是管理员界面，只能注册买家或者卖家（可以向buyer和seller中插入数据）
按4退出
 */

public class Controls {
    public void controls() {
        Meau meau = new Meau();
        meau.meau();
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        switch (num) {
            case 1:
                buyer();
                break;
            case 2:
                seller();
                break;
            case 3:
                admin();
                break;
            case 4:
                break;
            default:
                break;
        }
    }

    /*
    跳转到买家二级菜单
    可以在二级菜单中进行查看商品，购买商品，查看余额，充值余额操作
     */
    private void buyer() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入买家姓名:");
        String name = sc.nextLine();
        System.out.println("请输入买家密码:");
        String password = sc.nextLine();

        JdbcUtil util = new JdbcUtil();
        String sql = "SELECT * FROM buyer WHERE name = ? AND password = ?";
        try (Connection conn = util.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Buyercontrols buyer = new Buyercontrols();
                buyer.purchase();
            } else {
                System.out.println("用户名或密码错误，登录失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*
    跳转到卖家二级菜单
    可以在二级菜单中进行商品上架
     */
    private void seller() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入卖家姓名:");
        String name = sc.nextLine();
        System.out.println("请输入卖家密码:");
        String password = sc.nextLine();

        JdbcUtil util = new JdbcUtil();
        String sql = "SELECT * FROM seller WHERE name = ? AND password = ?";
        try (Connection conn = util.getConn();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, name);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                Sellcontrols seller = new Sellcontrols();
                seller.Shelf();
            } else {
                System.out.println("用户名或密码错误，登录失败！");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*
    跳转到管理员二级菜单
    可以在二级菜单中对买家和卖家进行注册（可以向buyer，seller插入数据）
     */
    private void admin() {
        Admincontrols admin = new Admincontrols();
        admin.usercontrol();
    }
}